/**
  ******************************************************************************
  * @file    app_database.h
  * @author  tuancheng_Zero
  * @version V0.1
  * @date    08/28/2024
  * @brief   database operate manage module
  ******************************************************************************
*/ 
#ifndef  __APP_DATABASE_H__
#define  __APP_DATABASE_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "includes.h"

#define DataBase_Page_Size                                          0xffff         // 数据库单页长度（字）
#define DataBase_MaxpageNum                                         32             // 数据库最大页面数
#define DataBase_UsedPageNum                                        11             // 数据库当前使用页面数
#define DataBase_CfgPageUsedNum                                     18             // 数据库配置页面使用页面数
#define DataBase_TmpPageUsedNum                                     2              // 数据库临时配置页面使用页面数

#define DataBase_TmpCfg_DeviceCtrlNum                               32             // 临时配置-设备控制大小
#define DataBase_TmpCfg_ProcParaNum                                 480            // 临时配置-过程参数大小

#define DataBase_DataArea_RAM                                       1
#define DataBase_DataArea_SRAM                                      2

#define DataBuff_ST 50


// 数据库数据页面映射表结构体
typedef struct{
    uint8_t * ramStartPoint;    // RAM数据开始内存地址
    uint8_t * sramStartPoint;   // SRAM数据开始地址存放指针
    uint16_t  ramDataLen;       // RAM数据的长度
    uint16_t  sramDataLen;      // SRAM数据的长度
}DataMapAddrTable_ST;

// 数据库配置页面映射结构体表
typedef struct{
    uint8_t * sramStartPoint;    // RAM数据开始内存地址
    uint16_t  dataValidLength;   // 数据有效长度
}ConfigMapAddrTable_ST;

extern ConfigMapAddrTable_ST cfgMapAddrTable[DataBase_CfgPageUsedNum];
extern ConfigMapAddrTable_ST tmpCfgMapAddrTable[DataBase_TmpPageUsedNum];
// 映射地址结构体
typedef struct{
    uint8_t dataArea;
    uint8_t * ramAddrPoint;
    uint32_t sramAddr;
}DataMapAddr_ST;

/* 数据库基础数据地址范围 */
#define DB_BaseAddrLen_RealTime                                     sizeof(share_RealTimeData_st)
#define DB_BaseAddrLen_EstaMagn                                     sizeof(share_EstaMagnData_st.baseData_st)
#define DB_BaseAddrLen_CeilingExci                                  sizeof(share_CeilingExciData_st.baseData_st)
#define DB_BaseAddrLen_Volatility                                   sizeof(share_VolatilityData_st.baseData_st)
#define DB_BaseAddrLen_UnderFreq                                    sizeof(share_UnderFreqData_st.baseData_st)
#define DB_BaseAddrLen_StartStop                                    sizeof(share_StartStopData_st.baseData_st)
#define DB_BaseAddrLen_Upload                                       sizeof(share_UploadData_st.baseData_st)
#define DB_BaseAddrLen_SuddenUnload                                 sizeof(share_SuddenUnloadData_st.baseData_st)
#define DB_BaseAddrLen_ParameterCfg                                 (DataBase_CfgPageUsedNum * EEPROM_PageSize_Div)
#define DB_BaseAddrLen_TempConfig                                   (DataBase_TmpPageUsedNum * TempCfg_PageSize_Div)

/* 数据库曲线数据地址范围 */
#define DB_CurvAddrLen_EstaMagn                                     (DB_BaseAddrLen_EstaMagn + 8000)
#define DB_CurvAddrLen_CeilingExci                                  (DB_BaseAddrLen_CeilingExci + 18000)
#define DB_CurvAddrLen_Volatility                                   (DB_BaseAddrLen_Volatility + 16000)              
#define DB_CurvAddrLen_UnderFreq                                    (DB_BaseAddrLen_UnderFreq + 8000)
#define DB_CurvAddrLen_StartStop                                    (DB_BaseAddrLen_StartStop + 8000)
#define DB_CurvAddrLen_Upload                                       (DB_BaseAddrLen_Upload + 16000)
#define DB_CurvAddrLen_SuddenUnload                                 (DB_BaseAddrLen_SuddenUnload + 16000)


/* 数据库数据地址映射表 */
#define DataBaseAddrMapTable {\
    {(uint8_t *)&share_RealTimeData_st,      (uint8_t *)NULL,                                                              DB_BaseAddrLen_RealTime,       0},\
    {(uint8_t *)&share_EstaMagnData_st,      (uint8_t *)&share_EstaMagnData_st.curveData_st.GenVoltDataCurve_Addr,         DB_BaseAddrLen_EstaMagn,       DB_CurvAddrLen_EstaMagn},\
    {(uint8_t *)&share_CeilingExciData_st,   (uint8_t *)&share_CeilingExciData_st.curveData_st.GenVoltDataCurve_Addr,      DB_BaseAddrLen_CeilingExci,    DB_CurvAddrLen_CeilingExci},\
    {(uint8_t *)&share_VolatilityData_st,    (uint8_t *)&share_VolatilityData_st.curveData_st.VoltDataCurve_U_Addr,        DB_BaseAddrLen_Volatility,     DB_CurvAddrLen_Volatility},\
    {(uint8_t *)&share_UnderFreqData_st,     (uint8_t *)&share_UnderFreqData_st.curveData_st.GenVoltDataCurve_Addr,        DB_BaseAddrLen_UnderFreq,      DB_CurvAddrLen_UnderFreq},\
    {(uint8_t *)&share_StartStopData_st,     (uint8_t *)&share_StartStopData_st.curveData_st.GenVoltCurve_Addr,            DB_BaseAddrLen_StartStop,      DB_CurvAddrLen_StartStop},\
    {(uint8_t *)NULL,                        (uint8_t *)NULL,                                                              0,                             0},\
    {(uint8_t *)&share_UploadData_st,        (uint8_t *)&share_UploadData_st.curveData_st.VoltDataCurve_U_Addr,            DB_BaseAddrLen_Upload,         DB_CurvAddrLen_Upload},\
    {(uint8_t *)&share_SuddenUnloadData_st,  (uint8_t *)&share_SuddenUnloadData_st.curveData_st.VoltDataCurve_U_Addr,      DB_BaseAddrLen_SuddenUnload,   DB_CurvAddrLen_SuddenUnload},\
    {(uint8_t *)&cfgMapAddrTable,            (uint8_t *)NULL,                                                              DB_BaseAddrLen_ParameterCfg,   0},\
    {(uint8_t *)&tmpCfgMapAddrTable,         (uint8_t *)NULL,                                                              DB_BaseAddrLen_TempConfig,     0},\
}

/* 数据库配置地址映射表 */
#define DataBaseCfgAddrMapTable {\
    {(uint8_t *)&share_parameterCfgData_st,                      sizeof(share_parameterCfgData_st.baseData_st) + 2},\
    {(uint8_t *)&share_parameterCfgData_st.nominalCfg_st,        sizeof(share_parameterCfgData_st.nominalCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.seriousAlarmCfg_st,   sizeof(share_parameterCfgData_st.seriousAlarmCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.pidDebugCfg_st,       sizeof(share_parameterCfgData_st.pidDebugCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.motorSwitchCfg_st,    sizeof(share_parameterCfgData_st.motorSwitchCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.f_BackClosingCfg_st,  sizeof(share_parameterCfgData_st.f_BackClosingCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.estaMagnCfg_st,       sizeof(share_parameterCfgData_st.estaMagnCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.volatilityCfg_st,     sizeof(share_parameterCfgData_st.volatilityCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.ceilingExciCfg_st,    sizeof(share_parameterCfgData_st.ceilingExciCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.underFreqCfg_st,      sizeof(share_parameterCfgData_st.underFreqCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.startStopCfg_st,      sizeof(share_parameterCfgData_st.startStopCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.syncClosingCfg_st,    sizeof(share_parameterCfgData_st.syncClosingCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.P100_uploadCfg_st,    sizeof(share_parameterCfgData_st.P100_uploadCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.P60_uploadCfg_st,     sizeof(share_parameterCfgData_st.P60_uploadCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.C60_uploadCfg_st,     sizeof(share_parameterCfgData_st.C60_uploadCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.suddenUnloadCfg_st,   sizeof(share_parameterCfgData_st.suddenUnloadCfg_st)},\
    {(uint8_t *)&share_parameterCfgData_st.calibrationFactor_st, sizeof(share_parameterCfgData_st.calibrationFactor_st)},\
    {(uint8_t *)&share_parameterCfgData_st.loadEquConfig_st,     sizeof(share_parameterCfgData_st.loadEquConfig_st)},\
}

/* 数据库临时配置地址映射表 */
#define DataBaseTmpCfgAddrMapTable {\
    {(uint8_t *)&share_TempConfig_st.deviceControl_st,         sizeof(share_TempConfig_st.deviceControl_st)},\
    {(uint8_t *)&share_TempConfig_st.procedurePara_st,         sizeof(share_TempConfig_st.procedurePara_st)},\
}

void DataBase_Init(void);
uint8_t DataBase_ReadDataContent(uint32_t address, uint16_t * dataBuff, uint16_t length);
uint8_t DataBase_WriteDataContent(uint32_t address, uint16_t * dataBuff, uint16_t length);
uint8_t DataBase_ProtocolContentRead(uint8_t *sourData, uint16_t sourDataLen, uint8_t *destData, uint16_t *destDataLen);
uint8_t DataBase_ProtocolContentWrite(uint8_t *sourData, uint16_t sourDataLen);
uint8_t ReadDataBaseDataContent(uint16_t address, uint16_t *Buffer, uint8_t Number);
uint16_t GetDataBaseDataContent(uint16_t address);
uint8_t WriteDataBaseDataContent(uint16_t address, uint16_t *Buffer, uint8_t Number);

#ifdef __cplusplus
}
#endif
#endif   /*   __EXTEND_EEPROM_H__   */




